package com.vecspace.web.servlet.model;

import com.vecspace.cache.LocalCache;
import com.vecspace.exception.HException;
import com.vecspace.utils.FileHelper;
import com.vecspace.utils.FreemarkerUtil;

public class PageParse {
	private static String cacheType = "ftltemplate";
	/**将FtlViewModel转为HTML文本
	 * @param viewModel
	 * @return
	 * @author: wanghua
	 */
	public static String parseViewModel(FtlViewModel viewModel) throws Exception{
		if(viewModel == null) {
			return null;
		}
		String filePath = viewModel.getViewPath();
		String text = LocalCache.DEFAULT().get(cacheType, filePath);		
		if(text == null || text.length() == 0){
			text = FileHelper.readFileText(filePath);
			if(text == null || text.length() == 0) {
				throw new HException("PageParse::没有读取到文件："+filePath);
			}
			LocalCache.DEFAULT().put(cacheType, filePath, text);
		}
		
		String html = FreemarkerUtil.templateParser(text, viewModel.getModel());
		return html;
	}
}
